import 'dart:core' as prefix0;
import 'dart:core';

import 'package:flustars/flustars.dart';

class DateUtils {
  static String SumDateForNow(String strDate) {
    if (strDate.contains("/")) {
      String year = strDate.substring(0, strDate.indexOf("/"));
      String month =
          strDate.substring(strDate.indexOf("/") + 1, strDate.lastIndexOf("/"));
      String day =
          strDate.substring(strDate.lastIndexOf("/") + 1, strDate.indexOf(" "));
      String hour =
          strDate.substring(strDate.indexOf(" ") + 1, strDate.indexOf(":"));
      String min =
          strDate.substring(strDate.indexOf(":") + 1, strDate.lastIndexOf(":"));
      String sec =
          strDate.substring(strDate.lastIndexOf(":") + 1, strDate.length);
      strDate =
          "$year-${month.padLeft(2, "0")}-${day.padLeft(2, "0")} ${hour.padLeft(2, "0")}:${min.padLeft(2, "0")}:${sec.padLeft(2, "0")}";
    }
    DateTime dateTime = DateUtil.getDateTime("$strDate");
//    DateTime now = DateTime.now().add(Duration(hours: 8));
    DateTime now = DateTime.now();
    Duration difference = now.difference(dateTime);
    if (dateTime.year < now.year) {
      return strDate.substring(0, 10);
    } else if (dateTime.month < now.month && difference.inDays > 7) {
      return strDate.substring(5, 10);
    } else if (difference.inDays > 0 && difference.inDays <= 7) {
      return "${difference.inDays + 1}天前";
    } else if (difference.inHours > now.hour) {
      return "昨天${strDate.substring(11, 16)}";
    } else if (difference.inMinutes > 30) {
      return "${strDate.substring(11, 16)}";
    } else if (difference.inMinutes > 0 && difference.inMinutes <= 30) {
      return "${difference.inMinutes + 1}分钟前";
    } else if (difference.inSeconds > 0) {
      return "${difference.inSeconds + 1}秒钟前";
    } else {
      return strDate;
    }
  }

  static String timestamp() {
    var date = DateTime.now().add(Duration(hours: 8));
    String timestamp =
        "${date.year.toString()}${date.month.toString().padLeft(2, '0')}${date.day.toString().padLeft(2, '0')}";
    return timestamp;
  }

  static String timestampSec() {
    var date = DateTime.now().add(Duration(hours: 8));
    String timestamp =
        "${date.year.toString()}${date.month.toString().padLeft(2, '0')}${date.day.toString().padLeft(2, '0')}${date.hour.toString().padLeft(2, '0')}${date.minute.toString().padLeft(2, '0')}${date.second.toString().padLeft(2, '0')}";
    return timestamp;
  }


  static String getStfDate(String date){
    var datetime = DateTime.parse(date);
    return "${datetime.year.toString()}年${datetime.month.toString()}月${datetime.day.toString()}日";
  }
  static String getMins(String time){
    int t = 0;
    int a = time.indexOf(" hour ");
    if(a!=-1){
      List l = time.split(" hour ");
      if(l.length == 2){
        t = int.parse(l[0]) * 60;
        String range = l[1].substring(
            0, l[1].length - 5);
        t+= int.parse(range);
      }
    }else{
      t = int.parse(time.substring(
          0, time.length - 5));
    }
    return "$t";
  }
}
